/
LSFmod.avsi
751 lines (681 loc) · 38 KB
/
LSFmod.avsi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
################################################################################################
### ###
### LimitedSharpenFaster MOD : function LSFmod() ###
### ###
### Modded Version by LaTo INV. ###
### ###
### v1.9 - 05 October 2009 ###
### ###
### another Modded Version by A.SONY. ###
### ###
### v2.193 - 17 May 2021 ###
### ###
################################################################################################
###
### +-----------+
### | CHANGELOG |
### +-----------+
###
### v2.193 : - replace UnsharpMask of Warpsharp to use UnsharpMask_avsi for HBD
###
### v2.192 : - added smode=6 (CAS) from vs port
###
### v2.191 : - optimise lut/expr with some ideas of VS port
###
### v2.190 : - fix and add new float parameters
###
### v2.189 : - remove unnecessary float clip clamp
###
### v2.188 : - fix (at least make it better) HBD when using high strength
###
### v2.187 : - fix bug in show=true
###
### v2.185 : - new avs26_optimize (bool) true by default, you can set it to false for smode=1 and smode=2 since not all filter updated to avs 2.6 colors
###
### v2.184 : - some fix when using soruce and make sure it will not work with RGB
###
### v2.183 : - fix bugs when show=true cases
###
### v2.18 : - fix upscale, i.e. dest_x or dest_y are larger than the input clip
###
### v2.12 : - HBD in avs+
### - some fixes
###
### v1.9 : - tweaked settings
### - default preset is now defaults="fast" /!\
###
### v1.8 : - changed preblur to allow more tweaking (bool->string)
### - tweaked settings
### - cleaned the code
### - updated documentation
###
### v1.7 : - changed Smethod=4 to "source"
###
### v1.6 : - added preblur option
### - added new Smethod=4
###
### v1.5 : - fixed LUT expression (thanks to Didée)
### - changed Smethod to Smethod+secure
###
### v1.4 : - changed defaults="new" to defaults="slow" & defaults="fast"
### - added show parameter
### - cleaned a little the code
###
### v1.3 : - changed a little Smethod=3&5 (same effect, but more precise)
### - added new calculation for soft (soft=-2) [default on]
### - added warning about bad settings (no more silent)
### - updated the documentation
###
### v1.2 : - added new Lmode<0 (limit with repair)
### - added 2 new Smode (unsharp masking)
### - changed Smode order: now old Smode3-4 is new Smode3-4 to avoid mistake
###
### v1.1 : - fixed a bug with dest_x!=ox or dest_y!=oy
### - replaced Lfactor by over/undershoot2
###
### v1.0 : - deleted old Smode(1-4), added new Smode(1-3) & Smethod(1-5)
### - added parameters for nonlinear sharpening (S2zp,S2pwr,S2dmpLo,S2dmpHi)
### - corrected the nonlinear formula
### - added new Lmode 2 & 4 + fixed Lmode 0
### - added faster edgemask
### - added soothe temporal stabilization, 2 parameters: soothe & keep
### - replaced lanczosresize by spline36resize
### - moved "strength" parameter (first place)
### - deleted wide, special and exborder
### - changed some code (cosmetic)
### - added "defaults" parameter (to switch between original and modded version)
### - added documentation
###
###
###
### +--------------+
### | DEPENDENCIES |
### +--------------+
###
### -> Masktools (v2.2.15 or up)
### -> Removegrain (v1.0pre or up)
### -> UnsharpMask_avsi.avsi [for Smode=1]
### -> Variableblur (v0.30 or up) [for Smode=2]
### -> CAS [for Smode=6]
### -> if you use avs+ then make sure it's the last one
###
###
###
###
### +-------+
### | USAGE |
### +-------+
###
### LSFmod( strength, Smode, Smethod, kernel,
### preblur, secure, source,
### Szrp, Spwr, SdmpLo, SdmpHi,
### Lmode, overshoot, undershoot, overshoot2, undershoot2,
### soft, soothe, keep,
### edgemode, edgemaskHQ,
### ss_x, ss_y, dest_x, dest_y,
### show, screenW, screenH,
### defaults )
###
###
###
### +---------+
### | GENERAL |
### +---------+
###
### strength [int]
### --------------
### Strength of the sharpening
###
### Smode [int: 1,2,3,4,5,6]
### ----------------------
### Sharpen mode:
### =1 : Unsharp masking (from UnsharpMask_avsi)
### =2 : Unsharp masking (from variableblur)
### =3 : Range sharpening
### =4 : Nonlinear sharpening (original version)
### =5 : Nonlinear sharpening (corrected version)
### =6 : Contrast Adaptive Sharpening (from CAS)
###
### Smethod [int: 1,2,3]
### --------------------
### Sharpen method: (only used in Smode=3,4,5)
### =1 : 3x3 kernel
### =2 : Min/Max
### =3 : Min/Max + 3x3 kernel
###
### kernel [int: 11,12,19,20]
### -------------------------
### Kernel used in Smethod=1&3
### In strength order: + 19 > 12 >> 20 > 11 -
###
###
###
### +---------+
### | SPECIAL |
### +---------+
###
### preblur [string: "ON","OFF",...]
### --------------------------------
### Mode to avoid noise sharpening & ringing (only used in Smode=3,4,5,6)
### "ON" is sufficient to prevent ringing, but to prevent noise sharpening you should set your own denoiser
### Usage: LSFmod(preblur="YourFavoriteDenoiser()")
### Example: LSFmod(preblur="FFT3Dfilter(sigma=4,plane=0)")
###
###
### secure [bool]
### -------------
### Mode to avoid banding & oil painting (or face wax) effect of sharpening
### (only used in Smode=3,4,5,6)
###
### source [clip]
### -------------
### If source is defined, LSFmod doesn't sharp more a denoised clip than this source clip
### In this mode, you can safely set Lmode=0 & PP=off
### Usage: denoised.LSFmod(source=source)
### Example: last.FFT3Dfilter().LSFmod(source=last,Lmode=0,soft=0)
###
###
###
### +----------------------+
### | NONLINEAR SHARPENING |
### +----------------------+
###
### Szrp [int]
### ----------
### Zero Point:
### - differences below Szrp are amplified (overdrive sharpening)
### - differences above Szrp are reduced (reduced sharpening)
###
### Spwr [int]
### ----------
### Power: exponent for sharpener
###
### SdmpLo [int]
### ------------
### Damp Low: reduce sharpening for small changes [0:disable]
###
### SdmpHi [int]
### ------------
### Damp High: reduce sharpening for big changes [0:disable]
###
###
###
### +----------+
### | LIMITING |
### +----------+
###
### Lmode [int: ...,0,1,2,3,4]
### --------------------------
### Limit mode:
### <0 : Limit with repair (ex: Lmode=-1 --> repair(1), Lmode=-5 --> repair(5)...)
### =0 : No limit
### =1 : Limit to over/undershoot
### =2 : Limit to over/undershoot on edges and no limit on not-edges
### =3 : Limit to zero on edges and to over/undershoot on not-edges
### =4 : Limit to over/undershoot on edges and to over/undershoot2 on not-edges
###
### overshoot [float]
### ---------------
### Limit for pixels that get brighter during sharpening
###
### undershoot [float]
### ----------------
### Limit for pixels that get darker during sharpening
###
### overshoot2 [float]
### ----------------
### Same as overshoot, only for Lmode=4
###
### undershoot2 [float]
### -----------------
### Same as undershoot, only for Lmode=4
###
###
###
### +-----------------+
### | POST-PROCESSING |
### +-----------------+
###
### soft [int: -2,-1,0...100]
### -------------------------
### Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate)
###
### soothe [bool]
### -------------
### =true : Enable soothe temporal stabilization
### =false : Disable soothe temporal stabilization
###
### keep [int: 0...100]
### -------------------
### Minimum percent of the original sharpening to keep (only with soothe=true)
###
###
###
### +-------+
### | EDGES |
### +-------+
###
### edgemode [int: -1,0,1,2]
### ------------------------
### =-1 : Show edgemask
### = 0 : Sharpening all
### = 1 : Sharpening only edges
### = 2 : Sharpening only not-edges
###
### edgemaskHQ [bool]
### -----------------
### =true : Original edgemask
### =false : Faster edgemask
###
###
###
### +------------+
### | UPSAMPLING |
### +------------+
###
### ss_x ; ss_y [float]
### -------------------
### Supersampling factor (reduce aliasing on edges)
###
### dest_x ; dest_y [int]
### ---------------------
### Output resolution after sharpening (avoid a resizing step)
###
###
###
### +-------+
### | DEBUG |
### +-------+
###
### show [bool]
### -----------
### Show debug clip & informations
###
### screenW ; screenH [int]
### -----------------------
### Screen resolution (for show clip)
###
###
###
### +----------+
### | SETTINGS |
### +----------+
###
### defaults [string: "old" or "slow" or "fast"]
### --------------------------------------------
### = "old" : Reset settings to original version (output will be THE SAME AS LSF)
### = "slow" : Enable SLOW modded version settings
### = "fast" : Enable FAST modded version settings
### --> /!\ [default:"fast"]
###
###
### defaults="old" : - strength = 100
### ---------------- - Smode = 3
### - Smethod = Smode==3?2:1
### - kernel = 11
###
### - preblur = "OFF"
### - secure = false
### - source = undefined
###
### - Szrp = 16
### - Spwr = 2
### - SdmpLo = strength/25
### - SdmpHi = 0
###
### - Lmode = 1
### - overshoot = 1
### - undershoot = overshoot
### - overshoot2 = overshoot*2
### - undershoot2 = overshoot2
###
### - soft = 0
### - soothe = false
### - keep = 25
###
### - edgemode = 0
### - edgemaskHQ = true
###
### - ss_x = Smode==3?1.50:1.25
### - ss_y = ss_x
### - dest_x = ox
### - dest_y = oy
###
### - show = false
### - screenW = 1280
### - screenH = 1024
###
###
### defaults="slow" : - strength = 100
### ----------------- - Smode = 5
### - Smethod = 3
### - kernel = 11
###
### - preblur = "OFF"
### - secure = true
### - source = undefined
###
### - Szrp = 16
### - Spwr = 4
### - SdmpLo = 4
### - SdmpHi = 48
###
### - Lmode = 4
### - overshoot = strength/100
### - undershoot = overshoot
### - overshoot2 = overshoot*2
### - undershoot2 = overshoot2
###
### - soft = -2
### - soothe = true
### - keep = 20
###
### - edgemode = 0
### - edgemaskHQ = true
###
### - ss_x = 1.50
### - ss_y = ss_x
### - dest_x = ox
### - dest_y = oy
###
### - show = false
### - screenW = 1280
### - screenH = 1024
###
###
### defaults="fast" : - strength = 100
### ----------------- - Smode = 3
### - Smethod = 2
### - kernel = 11
###
### - preblur = "OFF"
### - secure = true
### - source = undefined
###
### - Szrp = 16
### - Spwr = 4
### - SdmpLo = 4
### - SdmpHi = 48
###
### - Lmode = 1
### - overshoot = strength/100
### - undershoot = overshoot
### - overshoot2 = overshoot*2
### - undershoot2 = overshoot2
###
### - soft = 0
### - soothe = true
### - keep = 20
###
### - edgemode = 0
### - edgemaskHQ = false
###
### - ss_x = 1.25
### - ss_y = ss_x
### - dest_x = ox
### - dest_y = oy
###
### - show = false
### - screenW = 1280
### - screenH = 1024
###
################################################################################################
function LSFmod( clip inclip, float "strength", int "Smode", int "Smethod", int "kernel",
\ string "preblur", bool "secure", clip "source",
\ int "Szrp", float "Spwr", float "SdmpLo", float "SdmpHi",
\ int "Lmode", float "overshoot", float "undershoot", float "overshoot2", float "undershoot2",
\ int "soft", bool "soothe", int "keep",
\ int "edgemode", bool "edgemaskHQ",
\ float "ss_x", float "ss_y", int "dest_x", int "dest_y",
\ bool "show", int "screenW", int "screenH",
\ string "defaults", bool "avs26_optimize" )
{
sisphbd = AvsPlusVersionNumber > 2294
### DEFAULTS
version = "v2.193"
defaults = default(defaults,"fast")
num = defaults=="old" ? 0 : defaults=="slow" ? 1 : defaults=="fast" ? 2 : 3
ox = inclip.width
oy = inclip.height
Assert(num != 3 ? true : false, chr(10) + """Defaults must be "old" or "slow" or "fast" !""" + chr(10))
strength = default( strength, Select(num, 100 , 100 , 100 ) )
Smode = default( Smode, Select(num, 3 , 5 , 3 ) )
Smethod = default( Smethod, Select(num, Smode==3?2:1 , 3 , 2 ) )
kernel = default( kernel, Select(num, 11 , 11 , 11 ) )
preblur = default( preblur, Select(num, "OFF" , "OFF" , "OFF" ) )
secure = default( secure, Select(num, false , true , true ) )
Szrp = default( Szrp, Select(num, 16 , 16 , 16 ) )
Spwr = default( Spwr, Select(num, 2 , 4 , 4 ) )
SdmpLo = default( SdmpLo, Select(num, strength/25 , 4 , 4 ) )
SdmpHi = default( SdmpHi, Select(num, 0 , 48 , 48 ) )
Lmode = default( Lmode, Select(num, 1 , 4 , 1 ) )
overshoot = default( overshoot, Select(num, 1 , strength/100 , strength/100 ) )
undershoot = default( undershoot, Select(num, overshoot , overshoot , overshoot ) )
overshoot2 = default( overshoot2, Select(num, overshoot*2 , overshoot*2 , overshoot*2 ) )
undershoot2 = default( undershoot2, Select(num, overshoot2 , overshoot2 , overshoot2 ) )
soft = default( soft, Select(num, 0 , -2 , 0 ) )
soothe = default( soothe, Select(num, false , true , true ) )
keep = default( keep, Select(num, 25 , 20 , 20 ) )
edgemode = default( edgemode, Select(num, 0 , 0 , 0 ) )
edgemaskHQ = default( edgemaskHQ, Select(num, true , true , false ) )
ss_x = default( ss_x, Select(num, Smode==6?1.00:Smode==3?1.50:1.25 , 1.50 , 1.25 ) )
ss_y = default( ss_y, Select(num, ss_x , ss_x , ss_x ) )
dest_x = default( dest_x, Select(num, ox , ox , ox ) )
dest_y = default( dest_y, Select(num, oy , oy , oy ) )
show = default( show, Select(num, false , false , false ) )
screenW = default( screenW, Select(num, 1280 , 1280 , 1280 ) )
screenH = default( screenH, Select(num, 1024 , 1024 , 1024 ) )
Assert(!isRGB(inclip), chr(10) + "LSFmod: not work with an RGB clip !" + chr(10))
Assert( ( strength >= 0 ) ? true : false, chr(10) + "'strength' have not a correct value! [>=0]" + chr(10))
Assert( ( Smode >= 1 && Smode <= 6 ) ? true : false, chr(10) + "'Smode' have not a correct value! [1,2,3,4,5]" + chr(10))
Assert( ( Smethod >= 1 && Smethod <= 4 ) ? true : false, chr(10) + "'Smethod' have not a correct value! [1,2,3,4]" + chr(10))
Assert( ( kernel == 19 || kernel == 20 || kernel == 11 || kernel == 12 ) ? true : false, chr(10) + "'kernel' have not a correct value! [19,20,11,12]" + chr(10))
Assert( ( Szrp >= 1 && Szrp <= 255 ) ? true : false, chr(10) + "'Szrp' have not a correct value! [1...255]" + chr(10))
Assert( ( Spwr >= 1 ) ? true : false, chr(10) + "'Spwr' have not a correct value! [>=1]" + chr(10))
Assert( ( SdmpLo >= 0 && SdmpLo <= 255 ) ? true : false, chr(10) + "'SdmpLo' have not a correct value! [0...255]" + chr(10))
Assert( ( SdmpHi >= 0 && SdmpHi <= 255 ) ? true : false, chr(10) + "'SdmpHi' have not a correct value! [0...255]" + chr(10))
Assert( ( Lmode <= 4 ) ? true : false, chr(10) + "'Lmode' have not a correct value! [...-1,0,1,2,3,4]" + chr(10))
Assert( ( overshoot >= 0 && overshoot <= 255 ) ? true : false, chr(10) + "'overshoot' have not a correct value! [0...255]" + chr(10))
Assert( ( undershoot >= 0 && undershoot <= 255 ) ? true : false, chr(10) + "'undershoot' have not a correct value! [0...255]" + chr(10))
Assert( ( overshoot2 >= 0 && overshoot2 <= 255 ) ? true : false, chr(10) + "'overshoot2' have not a correct value! [0...255]" + chr(10))
Assert( ( undershoot2 >= 0 && undershoot2 <= 255 ) ? true : false, chr(10) + "'undershoot2' have not a correct value! [0...255]" + chr(10))
Assert( ( soft >= -2 && soft <= 100 ) ? true : false, chr(10) + "'soft' have not a correct value! [-2,-1,0,1...100]" + chr(10))
Assert( ( keep >= 0 && keep <= 100 ) ? true : false, chr(10) + "'keep' have not a correct value! [0...100]" + chr(10))
Assert( ( edgemode >= -1 && edgemode <= 2 ) ? true : false, chr(10) + "'edgemode' have not a correct value! [-1,0,1,2]" + chr(10))
Assert( ( ss_x >= 1.0 ) ? true : false, chr(10) + "'ss_x' have not a correct value! [>=1.0]" + chr(10))
Assert( ( ss_y >= 1.0 ) ? true : false, chr(10) + "'ss_y' have not a correct value! [>=1.0]" + chr(10))
soft = soft!=-1 ? soft : sqrt( (((ss_x+ss_y)/2.0-1.0)*100.0) ) * 10
soft = soft!=-2 ? soft : int( (1.0+(2.0/(ss_x+ss_y))) * sqrt(strength) )
soft = soft<=100 ? soft : 100
xxs = round(ox*ss_x/8)*8
yys = round(oy*ss_y/8)*8
str = float(strength)/100.0
sisyuy2 = inclip.isyuy2()
sisavs26 = !(VersionNumber() < 2.6)
avs26op = default(avs26_optimize, true)
sisupscale = (dest_x != ox || dest_y != oy)
inclip = !sisupscale && sisyuy2 && sisphbd ? inclip.converttoyv16() : inclip
sislumaonly = avs26op ? sisphbd ? inclip.isy() : sisavs26 ? inclip.isy8() : true : true
input = sisupscale || sislumaonly ? inclip : sisphbd ? inclip.converttoy() : inclip.converttoy8()
sode = defined(source)
source = sode ? sisupscale || sislumaonly ? source : sisphbd ? source.converttoy() : source.converttoy8() : source
Assert(!isYUY2(input), chr(10) + "LSFmod: not work with an YUY2 clip !" + chr(10))
### SHARP
tmp = ss_x > 1.0 || ss_y > 1.0 ? input.spline36resize(xxs,yys) : input
pre = preblur=="OFF" ? tmp
\ : preblur=="ON" ? !sisavs26 ? tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
\ ,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
\ ,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=1,V=1) ,U=1,V=1) \
: tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
\ ,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
\ ,"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?",use_expr=2,U=1,V=1) ,U=1,V=1)
\ : eval("tmp."+preblur)
dark_limit = pre.mt_inpand(U=1,V=1)
bright_limit = pre.mt_expand(U=1,V=1)
minmaxavg = mt_average(dark_limit,bright_limit,U=1,V=1)
method = Smethod==1 ? pre.removegrain(kernel,-1)
\ : Smethod==2 ? minmaxavg
\ : minmaxavg.removegrain(kernel,-1)
method = secure==true ? !sisavs26 ? method.mt_lutxy(pre,"x y < x 1 + x y > x 1 - x ? ?",U=1,V=1) \
: method.mt_lutxy(pre,"x y < x 0.51 scaleb + x y > x 0.51 scaleb - x ? ?",use_expr=2,U=1,V=1)
\ : method
method = preblur=="OFF" ? method : tmp.mt_makediff(mt_makediff(pre,method,U=1,V=1),U=1,V=1)
normsharp = Smode==1 ? tmp.unsharpmask_avsi(strength=str*100,radius=round(1.0+((ss_x+ss_y)/2.0)),threshold=0)
\ : Smode==2 ? tmp.unsharp(strength=str,vary=((ss_x+ss_y)/2.0),varc=1,border=3,U=1,V=1)
\ : Smode==3 ? !sisavs26 ? mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",U=1,V=1) \
: mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",use_expr=2,U=1,V=1)
\ : Smode==4 ? !sisavs26 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - 2 ^ x y - 2 ^ "
\ +string(SdmpLo)+" + / * x y - x y - abs / * 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / + ?",U=1,V=1) \
: mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" scalef / 1 "+string(Spwr)+" / ^ "+string(Szrp) +" scalef * "+string(str)+" * x y - dup * dup "
\ +string(SdmpLo)+" scalef scalef + / * x y - dup abs / * 1 "+string(SdmpHi)+" scalef 0 == 0 x y - abs "+string(SdmpHi)+" scalef / 4 ^ ? + / + ?",use_expr=2,U=1,V=1)
\ : Smode==5 ? !sisavs26 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - x y - abs / * x y - 2 ^ "
\ +string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x y - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "
\ +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1) \
: mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - dup abs / * x y - dup * "
\ +string(Szrp)+" dup * "+string(SdmpLo)+" + * x y - dup * "+string(SdmpLo)+" + "+string(Szrp)+" dup * * / * 1 "
\ +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1,use_expr=2,scale_inputs="allf")
\ : preblur!="OFF" && secure ? tmp.mt_makediff(mt_makediff(pre,pre.cas(min(Str, 1)).mt_lutxy(pre,"x y < x 0.51 scaleb + x y > x 0.51 scaleb - x ? ?",use_expr=2,U=1,V=1),U=1,V=1),U=1,V=1)
\ : preblur!="OFF" ? tmp.mt_makediff(mt_makediff(pre,pre.cas(min(Str, 1)),U=1,V=1),U=1,V=1) : secure ? pre.cas(min(Str, 1)).mt_lutxy(pre,"x y < x 0.51 scaleb + x y > x 0.51 scaleb - x ? ?",use_expr=2,U=1,V=1) : pre.cas(min(Str, 1))
### LIMIT
normal = mt_clamp(normsharp, bright_limit, dark_limit, sisavs26 ? overshoot : round(overshoot), sisavs26 ? undershoot : round(undershoot), U=1, V=1)
second = mt_clamp(normsharp, bright_limit, dark_limit, sisavs26 ? overshoot2 : round(overshoot2), sisavs26 ? undershoot2 : round(undershoot2), U=1, V=1)
zero = mt_clamp(normsharp, bright_limit, dark_limit, 0, 0, U=1, V=1)
edge = edgemaskHQ ? mt_logic( tmp.mt_edge(thY1=0,thY2=255,"8 16 8 0 0 0 -8 -16 -8 4",U=1,V=1)
\ ,tmp.mt_edge(thY1=0,thY2=255,"8 0 -8 16 0 -16 8 0 -8 4",U=1,V=1)
\ ,"max",U=1,V=1)
\ : tmp.mt_edge(thY1=0,thY2=255,mode="min/max",U=1,V=1)
edge = edgemaskHQ ? !sisavs26 ? edge.mt_lut("x 128 / 0.86 ^ 255 *",U=1,V=1) \
: edge.mt_lut("x range_half / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",clamp_float=true,U=1,V=1)
\ : !sisavs26 ? edge.mt_lut("x 32 / 0.86 ^ range_max *",U=1,V=1) \
: edge.mt_lut("x 32 scalef / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",clamp_float=true,U=1,V=1)
limit1 = Lmode<0 ? normsharp.repair(tmp,abs(Lmode),-1)
\ : Lmode==0 ? normsharp
\ : Lmode==1 ? normal
\ : Lmode==2 ? mt_merge(normsharp, normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
\ : Lmode==3 ? mt_merge(normal, zero, edge.mt_inflate(U=1,V=1), U=1, V=1)
\ : mt_merge(second, normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
limit2 = edgemode==0 ? limit1
\ : edgemode==1 ? mt_merge(tmp,limit1,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
\ : mt_merge(limit1,tmp,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
### SOFT
sharpdiff = mt_makediff(tmp,limit2,U=1,V=1)
sharpdiff = !sisavs26 ? mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
\ "x 128 - abs y 128 - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",U=1,V=1) \
: mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
\ "x range_half - abs y range_half - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",use_expr=2,U=1,V=1)
PP1 = soft==0 ? limit2 : mt_makediff(tmp,sharpdiff,U=1,V=1)
### SOOTHE
diff = mt_makediff(tmp,PP1,U=1,V=1)
diff2 = diff.temporalsoften(1,255,0,32,2)
diff3 = !sisavs26 ? mt_lutxy(diff,diff2,"x 128 - y 128 - * 0 < x 128 - 100 / "+string(keep)+
\ " * 128 + x 128 - abs y 128 - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",U=1,V=1) \
: mt_lutxy(diff,diff2,"x range_half - y range_half - * 0 < x range_half - 100 / "+string(keep)+
\ " * range_half + x range_half - abs y range_half - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",use_expr=2,U=1,V=1)
PP2 = soothe ? mt_makediff(tmp,diff3,U=1,V=1) : PP1
### OUTPUT
out = sisupscale ? tmp.mergeluma(PP2).spline36resize(dest_x,dest_y)
\ : (ss_x > 1.0 || ss_y > 1.0) ? sisavs26 ? PP2.spline36resize(dest_x,dest_y) : input.mergeluma(PP2.spline36resize(dest_x,dest_y))
\ : sisavs26 ? PP2 : input.mergeluma(PP2)
src = !sode ? nop()
\ : sisupscale ? source.spline36resize(dest_x,dest_y)
\ : source
in = sisupscale ? input.spline36resize(dest_x,dest_y)
\ : input
shrpD = mt_makediff(in,out,U=1,V=1)
shrpL = sode ? !sisavs26 ? shrpD.sh_Padding(4,4,4,4).repair(mt_makediff(in,src,U=1,V=1).sh_Padding(4,4,4,4),1,-1,-1).crop(4,4,-4,-4,true).mt_lutxy(shrpD,"x 128 - abs y 128 - abs < x y ?",U=1,V=1) \
: shrpD.sh_Padding(4,4,4,4).repair(mt_makediff(in,src,U=1,V=1).sh_Padding(4,4,4,4),1,-1,-1).crop(4,4,-4,-4,true).mt_lutxy(shrpD,"x range_half - abs y range_half - abs < x y ?",use_expr=1,U=1,V=1) : nop()
output = sode ? mt_makediff(in,shrpL,U=2,V=2)
\ : out
output = edgemode!=-1 ? output
\ : sisavs26 && !sisupscale ? edge.spline36resize(dest_x,dest_y) : edge.spline36resize(dest_x,dest_y).greyscale()
### SHOW
function LSFM_Show(clip input, clip output, string "version", string "defaults",
\ float "strength", int "Smode", int "Smethod", int "kernel",
\ string "preblur", bool "secure", string "source",
\ float "Szrp", float "Spwr", float "SdmpLo", float "SdmpHi",
\ int "Lmode", float "overshoot", float "undershoot", float "overshoot2", float "undershoot2",
\ int "soft", bool "soothe", int "keep",
\ int "edgemode", bool "edgemaskHQ",
\ float "ss_x", float "ss_y", int "dest_x", int "dest_y",
\ bool "show", int "screenW", int "screenH")
{
resoW = screenW - 384
resoH = screenH
inW = input.width()
inH = input.height()
outW = round( resoW / 4 ) * 4
outH = round( (outW * inH) / (4 * inW) ) * 4
scale = resoH - ( 2 * outH )
i = input.spline36resize(outW,outH)
i = scale > 0 ? i.addborders(0,scale/4,0,scale/4) : i
i = scale < 0 ? i.crop(0,-scale/4,0,scale/4,true) : i
o = output.spline36resize(outW,outH)
o = scale > 0 ? o.addborders(0,scale/4,0,scale/4) : o
o = scale < 0 ? o.crop(0,-scale/4,0,scale/4,true) : o
e = blankclip(input, width=384, height=resoH)
e = e.SubTitle("LSFmod " + string(version) ,text_color=$FFFFFF,font="COURIER NEW",size=24,x=40,y=40)
e = e.SubTitle("DEFAULTS = " + string(defaults) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=100)
e = e.SubTitle("GENERAL:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=150)
e = e.SubTitle("--------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=160)
e = e.SubTitle("strength = " + string(strength) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=180)
e = e.SubTitle("Smode = " + string(Smode) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=190)
e = e.SubTitle("Smethod = " + string(Smethod) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=200)
e = e.SubTitle("kernel = " + string(kernel) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=210)
e = e.SubTitle("SPECIAL:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=250)
e = e.SubTitle("--------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=260)
e = e.SubTitle("preblur = " + string(preblur) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=280)
e = e.SubTitle("secure = " + string(secure) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=290)
e = e.SubTitle("source = " + string(source) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=300)
e = e.SubTitle("NONLINEAR SHARPENING:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=340)
e = e.SubTitle("---------------------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=350)
e = e.SubTitle("Szrp = " + string(Szrp) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=370)
e = e.SubTitle("Spwr = " + string(Spwr) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=380)
e = e.SubTitle("SdmpLo = " + string(SdmpLo) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=390)
e = e.SubTitle("SdmpHi = " + string(SdmpHi) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=400)
e = e.SubTitle("LIMITING:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=440)
e = e.SubTitle("---------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=450)
e = e.SubTitle("Lmode = " + string(Lmode) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=470)
e = e.SubTitle("overshoot = " + string(overshoot) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=480)
e = e.SubTitle("undershoot = " + string(undershoot) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=490)
e = e.SubTitle("overshoot2 = " + string(overshoot2) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=500)
e = e.SubTitle("undershoot2 = " + string(undershoot2) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=510)
e = e.SubTitle("POST-PROCESSING:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=550)
e = e.SubTitle("----------------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=560)
e = e.SubTitle("soft = " + string(soft) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=580)
e = e.SubTitle("soothe = " + string(soothe) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=590)
e = e.SubTitle("keep = " + string(keep) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=600)
e = e.SubTitle("EDGES:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=640)
e = e.SubTitle("------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=650)
e = e.SubTitle("edgemode = " + string(edgemode) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=670)
e = e.SubTitle("edgemaskHQ = " + string(edgemaskHQ) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=680)
e = e.SubTitle("UPSAMPLING:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=720)
e = e.SubTitle("-----------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=730)
e = e.SubTitle("ss_x = " + string(ss_x) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=750)
e = e.SubTitle("ss_y = " + string(ss_y) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=760)
e = e.SubTitle("dest_x = " + string(dest_x) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=770)
e = e.SubTitle("dest_y = " + string(dest_y) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=780)
e = e.SubTitle("DEBUG:" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=820)
e = e.SubTitle("------" ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=830)
e = e.SubTitle("show = " + string(show) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=850)
e = e.SubTitle("screenW = " + string(screenW) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=860)
e = e.SubTitle("screenH = " + string(screenH) ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=870)
debug = stackhorizontal( e ,
\ stackvertical( i.subtitle("Input",text_color=$FFFFFF,size=20,x=40,y=20) ,
\ o.subtitle("Output",text_color=$FFFFFF,size=20,x=40,y=20) )
\ )
return debug
}
output = sisupscale || sislumaonly ? output : sisphbd ? CombinePlanes(output,inclip,planes="YUV",sample_clip=inclip) : ytouv(inclip.utoy8(),inclip.vtoy8(),output)
output = show==false ? output
\ : LSFM_Show(inclip, output, version, defaults,
\ strength, Smode, Smethod, kernel,
\ preblur, secure, sode?"defined":"undefined",
\ Szrp, Spwr, SdmpLo, SdmpHi,
\ Lmode, overshoot, undershoot, overshoot2, undershoot2,
\ soft, soothe, keep,
\ edgemode, edgemaskHQ,
\ ss_x, ss_y, dest_x, dest_y,
\ show, screenW, screenH)
output = sisyuy2 ? output.converttoyuy2() : output
return output
}